package com.zzl.admin.controller.base;

import com.zzl.admin.common.DateEditor;
import com.zzl.admin.entity.admin.User;
import com.zzl.admin.utils.EmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Description：基础Controller
 * Created by 朱正磊 on 2019-03-21 23:57
 */
@SuppressWarnings("ALL")
public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    /**
     * description：表单请求数据处理（其实就是处理字符串，与Javabean进行绑定）
     * user 朱正磊
     * time 2019-03-27 14:34
     * @param webDataBinder 数据绑定对象
     */
    @InitBinder
    protected void initBinder(WebDataBinder webDataBinder) {
        // 去字符串两边空格
        webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        // 字符串与日期绑定
        // TODO: 2019-03-27 有待验证
        webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    /**
     * description：获取Session
     * user 朱正磊
     * time 2019-03-27 16:54
     * @return 返回Session
     */
    protected Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * description：获取当前登录用户
     * user 朱正磊
     * time 2019-03-27 17:06
     * @return 返回当前登录用户
     */
    protected User getUser() {
        return (User) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * description：获取分页请求（默认排序）
     * user 朱正磊
     * time 2019-03-22 15:06
     * @return 返回分页对象
     */
    protected PageRequest getPageRequest() {
        int page = 0; //第几页
        int size = 10; //每页记录数
        Sort sort = null; //排序对象

        //获取页数、每页记录数、排序规则
        try {
            String sortName = request.getParameter("sortName"); //排序字段
            String sortOrder = request.getParameter("sortOrder"); //排序规则
            if (EmptyUtil.isNotEmpty(sortName) && EmptyUtil.isNotEmpty(sortOrder)) {
                if ("desc".equalsIgnoreCase(sortOrder)) { //倒序
                    sort = Sort.by(Sort.Direction.DESC, sortName);
                } else {
                    sort = Sort.by(Sort.Direction.ASC, sortName);
                }
            }
            page = Integer.valueOf(request.getParameter("pageNumber")) - 1;
            size = Integer.valueOf(request.getParameter("pageSize"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取分页对象
        PageRequest pageRequest;
        if (EmptyUtil.isNull(sort)) {
            pageRequest = PageRequest.of(page, size);
        } else {
            pageRequest = PageRequest.of(page, size, sort);
        }
        return pageRequest;
    }

    /**
     * description：获取分页请求（自定义排序）
     * user 朱正磊
     * time 2019-03-22 15:06
     * @param sort 排序对象
     * @return 返回分页对象
     */
    protected PageRequest getPageRequest(Sort sort) {
        int page = 0; //第几页
        int size = 10; //每页记录数

        //如果排序对象为空
        if (EmptyUtil.isEmpty(sort)) {
            return this.getPageRequest();
        }

        //获取页数、每页记录数、排序规则
        try {
            String sortName = request.getParameter("sortName"); //排序字段
            String sortOrder = request.getParameter("sortOrder"); //排序规则
            if (EmptyUtil.isNotEmpty(sortName) && EmptyUtil.isNotEmpty(sortOrder)) {
                if ("desc".equalsIgnoreCase(sortOrder)) { //倒序
                    sort = sort.and(Sort.by(Sort.Direction.DESC, sortName));
                } else {
                    sort = sort.and(Sort.by(Sort.Direction.ASC, sortName));
                }
            }
            page = Integer.valueOf(request.getParameter("pageNumber")) - 1;
            size = Integer.valueOf(request.getParameter("pageSize"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return PageRequest.of(page, size, sort);
    }

}
